import { ref } from 'vue'
import { getCheckFlowTemplateDetailWithProcessByTemplateId } from '@/api/basic'

/**
 * 审批流模板与流程详情服务
 */
export default function () {
  const getCheckFlowTemplateWithProcessDetailLoading = ref(false)
  const checkFlowTemplateWithProcessDetail =
    ref<BaseInfoAPIResponse.CheckFlowTemplateWithProcessDetail>({
      flowTemplateEntity: {
        code: '',
        description: '',
        gmtCreate: '',
        gmtUpdate: '',
        id: '',
        name: '',
      },
      flowTemplateNodeRelationList: [],
    })

  const getCheckFlowTemplateWithProcessDetail = async (templateId: string) => {
    try {
      getCheckFlowTemplateWithProcessDetailLoading.value = true
      const res = await getCheckFlowTemplateDetailWithProcessByTemplateId(templateId)
      getCheckFlowTemplateWithProcessDetailLoading.value = false
      if (res.data.code === 20000) {
        checkFlowTemplateWithProcessDetail.value = res.data.data
        return ''
      }

      return res.data.message
    } catch (e) {
      getCheckFlowTemplateWithProcessDetailLoading.value = false
      return '获取流程失败，请稍后重试'
    }
  }

  return {
    checkFlowTemplateWithProcessDetail,
    getCheckFlowTemplateWithProcessDetailLoading,
    getCheckFlowTemplateWithProcessDetail,
  }
}
